﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using BSDS.Commands;
using BSDS.Commands.Initalization;
using BSDS.Core.Infrastructure.Logging;
using BSDS.Core.Intialization;

namespace BSDS.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            Logger logger = null;

            try
            {
                logger = new Logger("Console", new TraceSource("BSDS.Console"), new List<ILogConsumer>());
                logger.Log("Executing command: " + string.Join(" ", args), TraceEventType.Verbose);

                using (var bsdsEngine = new BsdsEngine())
                {
                    bsdsEngine.Initialize();

                    var dispatcher = new CommandsModule().Build(bsdsEngine.Services);
                    var consoleCommand = CommandInfo.Parse(args);

                    dispatcher.Dispatch(consoleCommand);
                }
            }
            catch (Exception e)
            {
                var message = string.Format("Error: \n{0}.", e);

                if (logger != null)
                {
                    logger.Log(message);
                }
                else
                {
                    System.Console.WriteLine(message);
                }
            }
        }
    }
}
